<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 桌号分类
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$store_id=$this->session('store_id');
if(!$store_id){
	exi('请先选择门店！','error');
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['store_id']=$store_id;
	$return['store']=pdo_get('jy_weishop_store',array('id'=>$store_id,'uniacid'=>$_SESSION['uniacid']),array('id','tableaction'));
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['group_id']){
		$params['group_id']=$_RQ['group_id'];
		$return['params']['group_id']=$_RQ['group_id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 50;
	$total='';
	$fields=array('id','title','group_id','status','busy','image','scanningnum','hebingid');
	$list=pdo_getslice('jy_weishop_table',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['groups']=pdo_getall('jy_weishop_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('id','title'),'id');
	$return['busys']=array(
		'0'=>'空闲',
		'1'=>'被占用',
	);
	if($list){
		if($_SESSION['wxapp_id']){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
		}
		foreach($list as &$item){
			$group = pdo_get('jy_weishop_table_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['group_id']),array('id','title'),'id');
			$item['groupname']=$group['title'];
			$item['title']=$item['title'];
			$item['action']='jy_weishop/store.totable//scanning=1&store_id='.$store_id.'&tableid='.$item['id'];
			$item['url']=custom_url('jy_weishop/store.totable//scanning=1&store_id='.$store_id.'&tableid='.$item['id'],true);
			$item['image']=tomedia($item['image']);
			if($_SESSION['wxapp_id']){
				$image='oss/'.$_SESSION['uniacid'].'/images/jy_weishop/totable/tableid'.$item['id'].'.jpg';
				$isadd=cfc('files')->source_check($image);
				if(!$isadd){
					$url=wxapp_links($item['action']);
					if($_SESSION['fromh5wxapp']){
						$url='/pages/index/index?url='.urlencode($item['action']);
					}
					$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
					$path='oss/'.$_SESSION['uniacid'].'/images/jy_weishop/totable';
					if(!file_exists(ROOT_AT.$path)){
						mkdir(ROOT_AT.$path,0700, true);
					}
					cfc('files')->source_save($res,'nochoose','png','二维码',$image,'',$url);
				}
				$item['wxappcode']=tomedia($image);
			}
			if($item['hebingid']){
				$item['hebingtitle']=pdo_getcolumn('jy_weishop_table',array('id'=>$item['hebingid'],'uniacid'=>$_SESSION['uniacid']),'title');
			}
		}
	}
	$return['list']=$list;
	$return['tip']='编辑商户--服务设置--预约功能，选择不填的预约类型，会自由切换下面的预约对象，比如餐饮就是桌台，KTV就是包间';
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['params']['group_id'] =0;
	$return['isfounder']=$_SESSION['isfounder']?1:0;
	$return['tableactions']=array(
		array('id'=>'jy_weishop/store.goodslist/','title'=>'默认设置')
	);
	$install = check_permission('jy_dish');
	if($install){
		$return['tableactions'][]=array('id'=>'jy_weidish/goodslist/','title'=>'小周餐厅');
	}
	$install = check_permission('jy_weidish_xibei');
	if($install){
		$return['tableactions'][]=array('id'=>'jy_weidish_xibei/goodslist/','title'=>'西贝在线点餐');
	}
	set_html('framess',$this->menustore());
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择桌号！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_weishop_table',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='clear'){
	//清台
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择桌号！','error');
	}
	pdo_update('jy_weishop_table',array('busy'=>0,'usernum'=>0,'nowuserid'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	pdo_update('jy_weishop_table',array('hebingid'=>0,'busy'=>0,'usernum'=>0,'nowuserid'=>0),array('hebingid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$oldorderparams=array('status'=>0,'store_id'=>$store_id,'uniacid'=>$_SESSION['uniacid'],'tableid'=>$id);
	pdo_update('jy_weishop_order',array('status'=>4),$oldorderparams);
	exi('清台成功！','','list');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_table',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['addtype']=0;
	}else{
		$item=array('title'=>'','addtype'=>0,'isadd'=>1);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回桌号列表'),
		),
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'radio','title'=>'添加方式','name'=>'addtype','options'=>'types','show'=>array('isadd'=>1)),
					array('type'=>'string','title'=>'桌台编号','name'=>'title','help'=>'','show'=>array('addtype'=>0)),
					array('type'=>'string','title'=>'桌台编号前缀','name'=>'fasttitle','help'=>'例如：桌号A01的前缀是A','show'=>array('addtype'=>1)),
					array('type'=>'unitstring','unit'=>'天','title'=>'添加数量','name'=>'num','show'=>array('addtype'=>1)),
					array('type'=>'select','title'=>'桌台类型','name'=>'group_id','help'=>'','options'=>'groups'),
					array('type'=>'image','title'=>'桌台图片','name'=>'image','help'=>''),
					array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>''),
				)
			),
		)
	);
	$return['groups']=pdo_getall('jy_weishop_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('id','title'),'id');
	$return['wxappgroups']=pdo_getall('jy_weishop_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('id','title'));
	$return['types']=array('单个添加','批量添加');
	$return['htmls']=$htmls;
	set_html('framess',$this->menustore());
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'group_id'=>intval($postdata['group_id']),
		'status'=>intval($postdata['status']),
		'image'=>trim($postdata['image']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_table',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		if($postdata['addtype']){
			if(!$postdata['fasttitle'] || !$postdata['num']){
				exi('请输入编号前缀，添加数量','error');
			}
			for($i=0;$i<$postdata['num'];$i++){
				$key=$i+1;
				if($key<10){
					$key='0'.$key;
				}
				$data['title']=$postdata['fasttitle'].$key;
				$data['uniacid']=$_SESSION['uniacid'];
		$data['store_id']=$store_id;
		pdo_insert('jy_weishop_table',$data);
			}
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['store_id']=$store_id;
			pdo_insert('jy_weishop_table',$data);
		}
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='hebing'){
	$postdata=get_postdata();
	$hetableid1=intval($postdata['hetableid1']);
	$hetableid2=intval($postdata['hetableid2']);
	pdo_update('jy_weishop_table',array('hebingid'=>$hetableid1,'busy'=>4),array('id'=>$hetableid2,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！','','list');
}
if($_RQ['op']=='getfanjie'){
	$tableid=intval($_RQ['tableid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 50;
	$total='';
	$fields=array('id','ordersn','price','tableid');
	$list=pdo_getslice('jy_weishop_order',array('ispay'=>1,'tableid'=>$tableid,'storeid'=>$store_id,'uniacid'=>$_SESSION['uniacid']),array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$lv){
		$lv['title']='单号：'.$lv['ordersn'].',单额：'.$lv['price'];
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='kaitai'){
	$tableid=intval($_RQ['tableid']);
	$usernum=intval($_RQ['usernum']);
	if(!$tableid){
		exi('请选择桌台！','error');
	}
	if(!$usernum){
		exi('请输入人数！','error');
	}
	pdo_update('jy_weishop_table',array('usernum'=>$usernum,'busy'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$tableid));
	exi('开台成功！','','list');
}
if($_RQ['op']=='getoldorder'){
	$orderid=intval($_RQ['orderid']);
	$oldorder=model('jy_weishop/order')->get_order($orderid);
	if($oldorder['tableid']){
			$table=pdo_get('jy_weishop_table',array('uniacid'=>$_SESSION['uniacid'],'id'=>$oldorder['tableid']),array('title','group_id'));
			$grouptitle=pdo_getcolumn('jy_weishop_table_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$table['group_id']),'title');
			$oldorder['tabletitle']=$table['title'].'('.$grouptitle.')';
	}
	if($oldorder){
		$goodsprice=0;
		foreach($oldorder['goods'] as $gv){
			$goodsprice +=$gv['total_price'];
		}
		$oldorder['goodsprice']=$goodsprice;
	}
	$oldorder['api_data']=json_decode($oldorder['api_data'],true);
	$oldorder['newprice']=$oldorder['price'];
	$return['oldorder']=$oldorder;
	exi($return);
}
if($_RQ['op']=='gaioldorder'){
	$postdata=get_postdata();
	$newprice=$postdata['newprice'];
	$id=intval($postdata['id']);
	pdo_update('jy_weishop_order',array('price'=>$newprice),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！','','list');
}
if($_RQ['op']=='getnew'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>$store_id,
		'is_get'=>0,
		'up_time >'=>time()-100,
	);
	$table=pdo_get('jy_weishop_table',$params);
	if($table['id']){
		pdo_update('jy_weishop_table',array('is_get'=>1),array('id'=>$table['id'],'uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id));
		exi($table);
	}else{
		exi('none');
	}
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_table',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}